﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.commands;
using System.Windows.Input;
using System.Windows;
using InHD.model;
using InHD.dataaccess;
using System.Windows.Controls;

namespace InHD.viewmodels
{
    class LoginViewModel : WorkspaceViewModel
    {
        #region Fields
        private Staff _staff;
        readonly StaffRepository _staffRepository;
        #endregion

        #region Constructor

        public LoginViewModel()
		{
            this._staff = Staff.CreateNewStaff();
            this._staffRepository = new StaffRepository();
            this._staffRepository.LoadAllStaffs();

            this.IsUnauthenticated = false;
		}

		#endregion

        #region Commands

        private RelayCommand _loginCommand;
        private RelayCommand _cancelCommand;

        public ICommand CancelCommand
        {
            get { return _cancelCommand ?? (_cancelCommand = new RelayCommand((param) => this.CancelLogin())); }
        }

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new RelayCommand(
                        (param) => this.Login(param),
                        (param) => this.CanLogin
                        );
                }
                return _loginCommand;
            }
        }

        private void Login(object param)
        {
            //// Check in staff repository
            var passwordBox = param as PasswordBox;
            this.Password = passwordBox.Password;

            this.CurrentUserUNK = this._staffRepository.IsUsernameExisting(ref this._staff);
            if (!string.IsNullOrEmpty(this.CurrentUserUNK))
            {
                //this.IsUnauthenticated = false;
                OnRequestClose();
            }
            else
            {
                this.IsUnauthenticated = true;
                base.OnPropertyChanged("IsUnauthenticated");
            }
        }

        private void CancelLogin()
        {
            //Application.Current.Shutdown();
            OnRequestClose();
        }

        bool CanLogin
        {
            get { return true; }
        }
        #endregion

        #region Presentation Properties

        public override string DisplayName
        {
            get
            {
                return "Login";
            }
        }

        #endregion // Presentation Properties

        #region Properties
        public string Username
        {
            get { return this._staff.Username; }
            set { this._staff.Username = value; }
        }

        public string Password
        {
            get { return this._staff.Password; }
            set { this._staff.Password = value; }
        }

        public string CurrentUserUNK
        {
            get;
            set;
        }

        public bool IsUnauthenticated { get; set; }

        public Staff CurrentStaff
        {
            get
            {
                return this._staff;
            }
        }
        #endregion
    }
}
